package com.huawei.hwid.core.b.a;

import android.content.Context;
import android.text.TextUtils;
import com.huawei.hms.framework.common.ContainerUtils;
import com.huawei.hms.framework.network.restclient.Response;
import com.huawei.hms.framework.network.restclient.RestClient;
import com.huawei.hms.framework.network.restclient.hwhttp.HttpContants;
import com.huawei.hms.framework.network.restclient.hwhttp.RequestBody;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.huawei.hwid.core.d.b.e;
import com.huawei.hwid.core.datatype.HwAccount;
import com.huawei.hwid.core.encrypt.b;
import com.huawei.hwid.d.a;
import java.io.IOException;
import java.net.URL;
import java.net.UnknownHostException;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;

public class c {
    public static Response a(Context context, a aVar, String str) throws IOException {
        if (aVar == null || context == null) {
            throw new IOException("IOException[Invalid request param.]:");
        } else if (com.huawei.hwid.core.b.a.a.c.FileType.equals(aVar.d())) {
            e.b("HttpUtil", "Start get." + aVar.getClass().getSimpleName(), true);
            return c(context, aVar, str);
        } else {
            e.b("HttpUtil", "Start post." + aVar.getClass().getSimpleName(), true);
            return b(context, aVar, str);
        }
    }

    private static Response b(Context context, a aVar, String str) throws IOException {
        e.b("HttpUtil", "Start http post.", true);
        String a = aVar.a(context);
        e.b("HttpUtil", "Global url: " + a, false);
        Object file = new URL(a).getFile();
        RestClient a2 = com.huawei.hwid.vermanager.c.a().a(context, a.replace(file, ""), 18080, 18443);
        if (a2 == null) {
            e.d("HttpUtil", "httpClient init Failed", true);
            throw new UnknownHostException("ERROR");
        }
        Map a3 = a(context, aVar);
        e.b("HttpUtil", "post1 start, heads=" + a3.toString(), false);
        d dVar = (d) a2.create(d.class);
        try {
            String str2;
            if (aVar.d().equals(com.huawei.hwid.core.b.a.a.c.URLType)) {
                String k = aVar.k();
                e.b("HttpUtil", "request.urlencode()  ", true);
                str2 = k;
            } else {
                str2 = aVar.j();
            }
            e.b("HttpUtil", "reqContent =" + str2, false);
            return dVar.a(a3, file, RequestBody.create((String) a3.get(HttpContants.KEY_CONTENT_TYPE), str2.getBytes("UTF-8"))).execute();
        } catch (IOException e) {
            e.d("HttpUtil", "IOException", true);
            throw new IOException("IOException[don't set proxy]:" + e.getClass().getSimpleName());
        }
    }

    private static Response c(Context context, a aVar, String str) throws IOException {
        e.b("HttpUtil", "Start http get.", true);
        String a = aVar.a(context);
        e.b("HttpUtil", "Global url: " + a, false);
        RestClient a2 = com.huawei.hwid.vermanager.c.a().a(context, a.replace(new URL(a).getFile(), ""), 18080, 18443);
        if (a2 == null) {
            e.d("HttpUtil", "httpClient init Failed", true);
            throw new UnknownHostException("ERROR");
        }
        e.b("HttpUtil", "post1 start, heads=" + a(context, aVar).toString(), false);
        try {
            return ((d) a2.create(d.class)).a(a).execute();
        } catch (IOException e) {
            e.d("HttpUtil", "IOException", true);
            throw new IOException("IOException[don't set proxy]:" + e.getClass().getSimpleName());
        }
    }

    public static Map<String, String> a(Context context, a aVar) {
        Map<String, String> hashMap = new HashMap();
        hashMap.put("Connection", "Keep-Alive");
        if (com.huawei.hwid.core.b.a.a.c.URLType.equals(aVar.d()) || com.huawei.hwid.core.b.a.a.c.FileType.equals(aVar.d())) {
            hashMap.put(HttpContants.KEY_CONTENT_TYPE, "application/x-www-form-urlencoded; charset=UTF-8");
        } else {
            hashMap.put(HttpContants.KEY_CONTENT_TYPE, "text/html; charset=UTF-8");
        }
        Object obj = "";
        HwAccount b = a.a(context).b();
        if (b != null) {
            obj = a.a(context).a(b.d());
        }
        hashMap.put(HwAccountConstants.EXTRA_COOKIE, obj);
        switch (aVar.l()) {
            case 0:
                hashMap.put("Authorization", String.valueOf(System.currentTimeMillis()));
                break;
            case 1:
                if (b == null) {
                    e.c("HttpUtil", "account is null ", true);
                    break;
                }
                obj = b.g();
                Object d = b.d();
                if (!(TextUtils.isEmpty(d) || TextUtils.isEmpty(obj))) {
                    String str = System.currentTimeMillis() + ":" + new SecureRandom().nextInt(1000);
                    hashMap.put("Authorization", "Digest user=" + d + "," + "nonce" + ContainerUtils.KEY_VALUE_DELIMITER + str + "," + "response" + ContainerUtils.KEY_VALUE_DELIMITER + b.a(str + ":" + aVar.n(), obj));
                    break;
                }
        }
        return hashMap;
    }
}
